package com.example.a429;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat; // 导入 SwitchCompat

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class PublishGoodsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_goods);
        ImageButton backButton = findViewById(R.id.back_button);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建跳转到 MainActivity 的 Intent
                Intent intent = new Intent(PublishGoodsActivity.this, MainActivity.class);
                // 启动 MainActivity
                startActivity(intent);
                // 关闭当前 Activity
                finish();

                Button publishButton = findViewById(R.id.publish_button);
                EditText goodsTitle = findViewById(R.id.goods_title);
                EditText goodsDescription = findViewById(R.id.goods_description);
                EditText goodsPrice = findViewById(R.id.goods_price);
                EditText phoneNumber = findViewById(R.id.phone_number);
                EditText wechatId = findViewById(R.id.wechat_id);
                RadioGroup tradeGroup = findViewById(R.id.trade_group);
                // 使用 SwitchCompat 类型
                SwitchCompat freeShippingSwitch = findViewById(R.id.free_shipping);

                publishButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String title = goodsTitle.getText().toString().trim();
                        String description = goodsDescription.getText().toString().trim();
                        String price = goodsPrice.getText().toString().trim();
                        String phone = phoneNumber.getText().toString().trim();
                        String wechat = wechatId.getText().toString().trim();
                        int selectedId = tradeGroup.getCheckedRadioButtonId();
                        boolean isFreeShipping = freeShippingSwitch.isChecked();

                        if (title.isEmpty() || description.isEmpty() || price.isEmpty()) {
                            Toast.makeText(PublishGoodsActivity.this, "请填写必要信息", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        // 这里可以添加将数据上传到服务器等实际发布逻辑
                        Toast.makeText(PublishGoodsActivity.this, "商品发布成功", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}